package org.bigjava.travel.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bigjava.travel.bean.Message;
import org.bigjava.travel.dao.MessageDao;
import org.bigjava.travel.service.MessageService;
import org.bigjava.travel.utils.PageParam;
import org.bigjava.travel.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    MessageDao messageDao;

    @Override
    public long count() throws Exception {
        return messageDao.count();
    }

    @Override
    public long countByUserId(Integer userId) throws Exception {
        return messageDao.countByUserId(userId);
    }

    @Override
    public Message findById(Integer id) throws Exception {
        return messageDao.findById(id);
    }

    @Override
    public List<Message> findList() throws Exception {
        return messageDao.findList();
    }

    @Override
    public void save(Message message) throws Exception {
        messageDao.save(message);
    }

    @Override
    public void update(Message message) throws Exception {
        messageDao.update(message);
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        messageDao.deleteById(id);
    }

    @Override
    public List<Message> findByPage(int currentPage, int pageSize, String query) {
        List<Message> list = new ArrayList<Message>();
        PageHelper.startPage(currentPage, pageSize);
        if (Tools.notEmpty(query)) {
            list = messageDao.findListByQuery("%" + query + "%");
        } else {
            list = messageDao.findList();
        }
        PageInfo<Message> pageInfo=new PageInfo<Message>(list);
        return pageInfo.getList();
    }

    @Override
    public PageParam<Message> findByPageByUserId(int currentPage, int pageSize, Integer userId)throws Exception {
        PageParam<Message> pageParam = new PageParam<>();
        Page page = PageHelper.startPage(currentPage, pageSize);
        List<Message> list = messageDao.findListByUserId(userId);
        pageParam.setResult(list);
        pageParam.setSize(page.getPages());
        pageParam.setCount(page.getTotal());
        pageParam.setPageNumber(currentPage);
        pageParam.setPageSize(pageSize);
        return pageParam;
    }
}
